<?php


namespace App\Http\Controllers;


use App\Models\Announcement;
use Illuminate\Http\Request;

class AnnouncementController extends Controller
{
    /**
     * 首页滚动公告，最新一条
     * @return \Illuminate\Http\JsonResponse
     */
    public function newest()
    {
        $data = Announcement::orderByDesc('id')->select(['id', 'abstract'])->first();
        return $this->success($data);
    }

    /**
     * 公告列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $data = Announcement::select(['id', 'title', 'abstract', 'created_at'])->orderByDesc('id')->paginate(10);
        return $this->success($data);
    }

    /**
     * 详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function details(Request $request)
    {
        throw_err(!$request->id, 'id不能为空');
        $data = Announcement::where('id', $request->id)->first();
        $data->read_num += 1;
        $data->save();
        return $this->success($data);
    }
}